package com.cmy.demo02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//使用该类,动态生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的接口,这里使用Object 实现动态可复用
    private Object target;
    public void setTarget(Object target) {
        this.target = target;
    }
    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                target.getClass().getInterfaces(),this);
    }
    //处理代理实例,并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log(method.getName());
        Object result = method.invoke(target,args);
        return result;
    }

    //添加日志功能
    public void log(String msg){
        System.out.println("[DEBUG]:"+msg+"方法被执行了");
    }
}

    